

local led = require("led")
local hmt = require("httpsvr")
local stgine = require("stgine")
local delay_timer = require("delay_timer")

local obj = {}

function obj:init()
  
    hmt:CreateServer(80)

    hmt:SetAct('led', function (req, res)
        print('data', req.data)
        if tonumber(req.data) == 1 then
            led:Open()
        else
            led:Close()
        end
        res:send(0)
    end)

    hmt:SetAct('setfx', function (req, res)
        print('data', req.data)
        stgine:SetFx(tonumber(req.data))
        res:send(0)
    end)

    local delaySwitch
    hmt:SetAct('setOn', function (req, res)
        if req.data.delay then
            delaySwitch = req.data.fx
            delay_timer:SetTimeOut(function ()
                stgine:SetOn(req.data.fx)
            end, tonumber(req.data.delay))
        else
            stgine:SetOn(req.data.fx)
        end
        res:send(0)
    end)

    hmt:SetAct('getState', function (req, res)
        local is_open, remain = delay_timer:GetStatus()
        res:send(0, {
            is_open = is_open,
            remain = remain,
            switch = delaySwitch
        })
    end)


    hmt:SetAct('setCfg', function (req, res)
        print('data', req.data)
        for key, value in pairs(req.data) do
            Config:Set(key, value)
        end
        res:send(0)
    end)
end

return obj